﻿@model QuanLySanXuat.Models.PurchaseFormModels
@{
    ViewBag.Title = "Nhập nguyên vật liệu";
}
<script type="text/javascript">
    $(document).ready(function () {
        $("#addline_CreateMaterial").click(function () {
            addTableRow();
            return false;
        });

        function RemoveRow(element) {
            var rowCount = $('#tblPurchaseDetail tbody tr').length;
            if (rowCount < 2) {
                alert("Để lại ít nhất 1 bản ghi");
                return;
            }
            else {
                var tr = $(element).closest('tr');
                tr.remove();
            }
        }

        $(".qty").change(function () {
            CalAmount(this);
            CalTotal();
        });

        $(".unitprice").change(function () {
            CalAmount(this);
            CalTotal();
        });

    });

    function CalAmount(element) {
        var tr = $(element).closest('tr');
        $(".amount", tr).val(function () {
            var qty = 0;
            if ($('.qty', tr).val() != "")
                qty = $('.qty', tr).asNumber();

            var unitprice = 0;
            if ($('.unitprice', tr).val() != "")
                unitprice = $('.unitprice', tr).asNumber();

            var retValue = qty * unitprice;
            return retValue;
        }).formatCurrency();
    }

    function CalTotal() {
        $("#txtPurchaseTotal").text(function () {
            var value = 0;
            $(".amount").each(function () {
                if ($(this).val() != "")
                    value += $(this).asNumber();
            });
            return value;
        }).formatCurrency();
    }

    function addTableRow() {
        var $tr = jQuery('table#tblPurchaseDetail').find("tbody tr:last").clone(true);
        $tr.find("input:not(:hidden),select").val('');
        jQuery('table#tblPurchaseDetail').find("tbody tr:last").after($tr);
    };

</script>
<div id="breadcrumb" >
    <div class="container">
        <div id="con_top">
            @*<div class="c_container">*@
                <h3>Cập nhập nguyên vật liệu</h3>
            @*</div>*@
        </div>
    </div>
</div>

@if (Model == null)
{
    <form class="form-horizontal" role="form" id="CheckValid" style="margin-top:10px;">
        <div class="col-md-6">
          <div class="form-group">
            <label class="col-sm-4 control-label">Số phiếu</label>
            <div class="col-sm-8">
              @Html.TextBox("txtInvoiceNo", "", new { @class = "required form-control" })
            </div>
          </div>
          <div class="form-group">
            <label class="col-sm-4 control-label">Người duyệt</label>
            <div class="col-sm-8">
              @Html.DropDownList("EmployeeID", new SelectList(ViewData["Employee"] as IEnumerable<Data.Models.Employee>, "ID", "Name"), "Chọn người duyệt", new { @class = "form-control" })
            </div>
          </div>       
            <div class="form-group">
                <label class="col-sm-4 control-label">Kho nhập</label>
                <div class="col-sm-8">
                  @Html.DropDownList("WarehouseID", new SelectList(ViewData["Warehouse"] as IEnumerable<Data.Models.Stock>, "ID", "Name"), "Chọn kho", new { @class = "form-control" })
                </div>
            </div>
          <div class="form-group">
            <label class="col-sm-4 control-label">Nhà cung cấp</label>
            <div class="col-sm-8">
              @Html.DropDownList("ContactID", new SelectList(ViewData["Contact"] as IEnumerable<Data.Models.Contact>, "ID", "ContactName"), "Chọn nhà cung cấp", new { @class = "form-control" })
            </div>
          </div>
          <div class="form-group">
            <label class="col-sm-4 control-label">Ghi chú</label>
            <div class="col-sm-8">
              @Html.TextBox("txtNote", "", new { @class = "required form-control" })
            </div>
          </div>
        </div>

        <div class="col-md-6">
          <div class="form-group">
            <label class="col-sm-3 control-label">Ngày lập phiếu</label>
            <div class="col-sm-8">
                @Html.TextBox("ContactName", String.Empty, new { @class = "required form-control" })
            </div>
          </div>
          <div class="form-group">
            <label class="col-sm-3 control-label">Ngày duyệt</label>
            <div class="col-sm-8">
                @Html.TextBox("txtDueDate", String.Empty, new { @class = "required form-control" })              
            </div>
          </div>

          <div class="form-group">
            <label class="col-sm-3 control-label">Ngày nhập kho</label>
            <div class="col-sm-8">
                @Html.TextBox("DateInWarehouse", String.Empty, new { @class = "required form-control" })
            </div>
          </div>
          <div class="form-group">
            <label class="col-sm-3 control-label">Loại tiền tệ</label>
            <div class="col-sm-8">
                @Html.DropDownList("CurrencyID", new SelectList(ViewData["Currency"] as IEnumerable<Data.Models.Currency>, "ID", "Name"), "Chọn loại tiền tệ", new { @class = "form-control" })
            </div>
          </div>
        </div>

        <div class="form-group">
            <div class="c_container col-sm-10 col-sm-offset-1">
                <div id="form_body">
                    <table id="tblPurchaseDetail" cellpadding="0" cellspacing="0" width="1072">
                      <thead>
                        <th width="240" class="paddingl20">Nguyên vật liệu</th>
                        <th width="80" class="paddingl20">Đơn vị tính</th>
                        <th width="65" class="paddingl20">Số lượng</th>
                        <th width="85" class="paddingl20">Đơn giá</th>
                        <th width="160" class="textaligncenter">Tổng tiền</th>
                        <th width="50" align="center" class="paddingl20"></th>
                      </thead>
                      <tbody id="insertRow">
                        @for (int iIndex = 0; iIndex < 5; iIndex++)
                        {
                          var className = (iIndex % 2 == 0) ? "even" : "old";
                          <tr class="@className">
                            <td class="paddingr5 paddingl5">
                              @*@Html.DropDownList("ddlGoodID", (IEnumerable<SelectListItem>)ViewData["GoodList"], new { @class = "goodchange form-control" })*@
                                @Html.DropDownList("MaterialID", new SelectList(ViewData["Material"] as IEnumerable<Data.Models.Material>, "ID", "Name"), "Chọn loại nguyên vật liệu", new { @class = "span2 col-sm-1 form-control" })
                            </td>
                            <td class="paddingr5 paddingl5">
                              @Html.DropDownList("MaterialUOMID", new SelectList(ViewData["UOM"] as IEnumerable<Data.Models.UOM>, "ID", "Name"), "Chọn đơn vị", new { @class = "span2 col-sm-1 form-control accesscorychange" })
                            </td>

                            <td class="paddingr5 paddingl5">
                              <input type="text" id="txtquantity" name="txtquantity" class="allownumericwithoutdecimal quantity qty text-right form-control" max-length="9" />
                            </td>
                            <td class="paddingr5 paddingl5">
                              <input type="text" id="txtUnitPrice" name="txtUnitPrice" data-value="1" class="numeric unitprice currency checkvalid form-control" />
                            </td>
                            <td class="paddingr5 paddingl5">
                              <input type="text" id="txtAmount" class="txt1 amount currency numeric form-control" readonly /></td>
                            <td align="center">
                              @*<i class="tmt_icon tmt_table_close" id="delRow" onclick="RemoveRow(this);"></i>*@
                                <div class="remove" id="delRow" onclick="RemoveRow(this);"></div>
                            </td>
                          </tr>
                        }
                      </tbody>
                    </table>
                </div>

                <div class="tablefooter">
                    <div class="pos1">
                        <input type="button" id="addline_CreateMaterial" class="addline" value="Thêm nguyên vật liệu">
                    </div>
                    <div class="col-md-3"></div>
                    <div class="col-md-5 bold18">
                            <p class="alert_total">
                                <label class="col-sm-8 control-label">Tổng cộng</label>
                                <span class="col-sm-4 price total" style="background: none; color: red;" id="txtPurchaseTotal">0.00</span>
                            </p>
                    </div>
                </div>
                <div class="cleardiv"></div>
                <div class="btn-group">
                    <div class="btn-group">
                        <button id="btnSaveSale" type="submit" class="btn btn-defaultxero">Ghi lại</button>
                    </div>
                </div>
                <div class="btn-group">
                    <div class="btn-group">
                        <button id="btnApprove" type="button" class="btn btn-defaultxero">Duyệt phiếu</button>
                    </div>
                </div>
                <div class="btn-group">
                    <button id="btnCancelSale" type="button" onclick="javascript: location.href = '/Purchase';" class="btn btn-defaultxero">Hủy bỏ</button>
                </div>
            </div>
        </div>
    </form>
}
